using System;
using System.IO;
using Castle.Core.Logging;
using Castle.Services.Logging.Log4netIntegration;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;

namespace Caramel.Castle
{
	public class LoggingInstaller : IWindsorInstaller
	{
		public LoggingInstaller()
		{
			this.LoggerFactory = new Log4netFactory();
		}

		public LoggingInstaller(string configFile)
		{
			this.LoggerFactory = new Log4netFactory(configFile);
		}

		public LoggingInstaller(Stream config)
		{
			this.LoggerFactory = new Log4netFactory(config);
		}

		private ILoggerFactory LoggerFactory { get; set; }

		public void Install(IWindsorContainer container, IConfigurationStore store)
		{
			container.
				Register(
					Component.
						For<ILoggerFactory>().
						UsingFactoryMethod(() => this.LoggerFactory).
						LifeStyle.
						Singleton,
					Component.
						For<ILogger>().
						UsingFactoryMethod(
							(k, x) =>
							k.
								Resolve<ILoggerFactory>().
								Create(x.Handler.ComponentModel.Implementation)
						).
						LifeStyle.
						Transient
				);
		}
	}
}
